﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace TestAnimation
{
    class Screen
    {
        private readonly Graphics g;
        private readonly int width;
        private readonly int height;
        private readonly ArrayList gObject;
        readonly ArrayList stars;
        private readonly double verticalRatio;
        private readonly double horizontalRatio;

        public Screen(Graphics g, Rectangle clientRectangle)
        {
            this.g = g;
            width = clientRectangle.Width;
            height = clientRectangle.Height;
            verticalRatio = (double) height/Globals.maxY;
            horizontalRatio = (double) width/Globals.maxX;
            g.ScaleTransform((float) horizontalRatio,(float) verticalRatio);
            g.SmoothingMode = SmoothingMode.HighSpeed;
            gObject = new ArrayList();
            stars = new ArrayList();
            for (int i = 0; i < Globals.stars; i++)
                stars.Add(new Point(Helper.GetRnd(Globals.maxX), Helper.GetRnd(Globals.maxY)));
        }
        public void Add(Object o) //Point, Segment, ArrayList, Circle
        {
            gObject.Add(o);
        }
        public void Draw()
        {
            g.FillRectangle(Brushes.Black, 0, 0, Globals.maxX, Globals.maxY); //clear the screen

            foreach (Point p in stars) //Draw the stars
            {
                int i = Helper.GetRnd(155) + 100;
                var pen = new Pen(Color.FromArgb(i, i, i));
                g.DrawEllipse(pen, p.X, p.Y, 1, 1);
            }
            foreach (var o in gObject) //Draw the objects
                {
                    if (o is Particle)
                    {
                        var particle = (Particle) o;
                        var p1 = particle.position;
                        g.DrawLine(particle.pen, p1.X, p1.Y, p1.X + 5, p1.Y + 5);
                    }
                    if (o is Bullet)
                    {
                        var bullet = (Bullet)o;
                        var p1 = bullet.position;
                        var pen = new Pen(Color.Red);
                        g.DrawRectangle(pen, p1.X, p1.Y, 20, 20);
                    }
                    else if (o is ArrayList)
                    {
                        int i = 0;
                        var arrL = (ArrayList) o;
                        var poly = new Point[arrL.Count];
                        var pen = new Pen(Color.White);
                        foreach (Point arr in arrL)
                        {
                            poly[i] = arr;
                            i++;
                        }
                        g.DrawPolygon(pen,poly);

                    }
                    else if (o is Explosion.Line)
                    {
                        var l = (Explosion.Line) o;
                        var pen = l.pen;
                        var p1 = l.point1.ToPoint();
                        var p2 = l.point2.ToPoint();
                        g.DrawLine(pen, p1, p2);
                    }
                    else if (o is Explosion.Circle)
                    {
                        var l = (Explosion.Circle)o;
                        var pen = l.pen;
                        var size = l.size * 10;
                        var p1 = l.point1.ToPoint();
                        g.DrawEllipse(pen, p1.X - size / 2, p1.Y - size / 2, size, size);
                    }
                }
            
            gObject.Clear();
        }

    }
    
}
